<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2020 幕小铺科技（大连）有限公司
// +----------------------------------------------------------------------
// | 2020/03/14
// +----------------------------------------------------------------------

namespace app\user_center\model\user;

use think\Model;

/**
 * 储值 流水模型
 * @package app\user_center\model
 */
class UcMemberBalanceJournalLog extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'xm_uc_member_balance_journal_log';
	
	/**
     * 添加流水
     * @param int $uid     用户id
     * @param string $journal_id 订单id
     * @param int $ine 类型 1充值 2赠送 3消费
     * @param decimal $price 金额
     * @param string $use_rule 规则
     * @param string $log_remark 备注
     * @author Nanuto 宝石
     * @return array
     */
	public function addData($uid, $journal_id, $ine, $price, $use_rule, $log_remark){
		$data['uid']                       = $uid;
        $data['journal_id']                = $journal_id;
        $data['ine']                       = $ine;
        $data['price']                     = $price;
        $data['use_rule']                  = $use_rule;
        $data['log_remark']                = $log_remark;
        $res = $this -> isUpdate(false) ->data($data, true) -> save();

        switch ($ine) {
            case 1://充值
                //余额更新
                $map = [];
                $map['uid'] = $uid;
                $res2 = model('user_center/Member', 'model\user') -> where($map) -> setInc('balance', $price);
                break;
            case 2://赠送
                //余额更新
                $map = [];
                $map['uid'] = $uid;
                $res2 = model('user_center/Member', 'model\user') -> where($map) -> setInc('balance', $price);
                break;
            case 3://消费
                //余额更新
                $map = [];
                $map['uid'] = $uid;
                $res2 = model('user_center/Member', 'model\user') -> where($map) -> setDec('balance', $price);
                break;
            default:
                # code...
                break;
        }
        if($res && $res2){
            return array_out(200, 'success', $this->id);
        }else{
            return array_out(-1001, 'error');
        }
	}

	
	/**
     * 用户统计 关联载入（一对一）
     */
    public function getUcMember(){
        return $this -> hasOne('app\user_center\model\user\Member','uid', 'uid');
    }

    /**
     * 关联储值订单
     */
    public function getUcMemberBalance(){
        return $this -> hasOne('app\user_center\model\user\UcMemberBalance','id', 'journal_id');
    }

    /**
     * 关联储值订单 （消费关联 订单）
     */
    public function getErpGoodsOrder(){
        return $this -> hasOne('app\erp\model\ErpGoodsOrder','id', 'journal_id');
    }

    /**
     * 删除数据
     * @param int $id
     * @author dou<51779987@qq.com>
     * @return bool
     */
    public function deleteData($id){
        $res = self::destroy($id);
        if($res){
            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
    }


}
